using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Read_the_Reader
{

    public static class NativeMethods
    {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern int RegisterWindowMessage(string msgName);
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("Shell32", CharSet = CharSet.Unicode)]
        internal static extern int ExtractIconEx(string sFile, int iIndex,  out IntPtr piLargeVersion,  out IntPtr piSmallVersion, int amountIcons); 


        internal static int UpdateInterval;
        internal static int Info;
        internal static int UpdateNow;

        static NativeMethods()
        {
            UpdateInterval = RegisterWindowMessage(Strings.ParmUpdateInterval);
            Info = RegisterWindowMessage(Strings.ParmInfo);
            UpdateNow = RegisterWindowMessage(Strings.ParmUpdateNow);
        }

        internal static bool SendMessage(string windowTitle, int msgId, IntPtr wParam, IntPtr lParam)
        {
            var window = FindWindow(null, windowTitle);
            if (window == IntPtr.Zero) return false;

            var result = SendMessage(window, msgId, wParam, lParam);

            return result == IntPtr.Zero;
        }

        internal static Icon GetExecutableIcon()
        {
            IntPtr large;
            IntPtr small;
            ExtractIconEx(Application.ExecutablePath, 0, out large, out small, 1);
            return Icon.FromHandle(small);
        }

    }
}
